package utility;
import java.util.*;
import dbmanager.*;
import java.sql.*;
import java.util.Collections;
public class ItemCodeSequenceGenerator
{
	public static Vector<String> seq = new Vector<String>();
	static int counter = 0;
	
	public ItemCodeSequenceGenerator() 
	{	
		if(counter==0)
		{
			createSequence();
			counter++;
		}
	}
	public Vector<String> createSequence()
	{
		char [] first = {'A','D','G','J','M','P','T','W','B','E','H','K','N'};
		char [] other={'A','D','G','J','M','P','T','W','B','E','H','K','N','Q','U','X'};
		boolean ChangeSecondChar = false;
		
		for(int i=0;i<first.length-1;i++)
		{
			
			StringBuffer sbr = new StringBuffer();//null
			sbr.append(first[i]);
			System.out.println(sbr.toString());
			for(int j=0;j<other.length;j++)
			{
				int diff1 = first[i]-other[j];
				System.out.println("The diff1 is "+diff1);
				if( diff1 == 0 || diff1 == -1 || diff1 == 1)
				{
					System.out.println(first[i]+" & "+other[j]+" Characters on same button");
					continue;
				}
				else
				{	
					if(sbr.length()<=2)
					{
						if(ChangeSecondChar == true)
						{
							String temp = sbr.subSequence(0, 1).toString();
							sbr = new StringBuffer();
							sbr.append(temp);
							sbr.append(other[j]);
							System.out.println(sbr.toString());
						}
						else
						{
							sbr.append(other[j]);
							System.out.println(sbr.toString());
						}
					}
					else
					{
						continue;
					}
					for(int k=0;k<other.length;k++)
					{
						int diff2 = other[j]-other[k];
						System.out.println("The diff2 is "+diff2);
						if( diff2 == 0 || diff2 == -1 || diff2 == 1)
						{
							System.out.println(other[j]+" & "+other[k]+" Characters on same button");
							continue;
						}
						else
						{	
							if(sbr.length()<3)
							{	
								sbr.append(other[k]);
								System.out.println(sbr.toString());
								seq.add(sbr.toString());
								String temp = sbr.subSequence(0,2).toString();
								sbr = new StringBuffer();
								sbr.append(temp);
								if(other[k]== 'K')
									ChangeSecondChar = true;
								else
									ChangeSecondChar = true;
							}
							else
							{
								continue;
							}
							
						}
					}
				}
			}
			
		}
		System.out.println(seq);
		
		System.out.println("\n"+"After Sorting the Vector !");
		Collections.sort(seq);
		System.out.println(seq);
		
		System.out.println(seq.size());
		return seq;
	}
	public static void main(String args[])
	{
		try
		{
			ItemCodeSequenceGenerator seq = new ItemCodeSequenceGenerator();
			Vector<String> FinalSeq =  seq.createSequence();
			
		/*	DBManager db = new DBManager("ItemList");
			db.getConnect();
			
			String query = "SELECT * FROM categorylist ";
			int i = 0;
			System.out.println("i is "+i);
			ResultSet rs = db.getSelect(query);
			while(rs.next())
			{
				int id = rs.getInt(1);
				System.out.println("The id is "+id);
				
				int varieties = rs.getShort("varieties");
				System.out.println("The varieties is "+varieties);
				
				if(varieties > 0 )
				{
					String subcatQuery = "UPDATE categorylist SET ItemCode = '"+FinalSeq.get(i)+"' WHERE ID ="+id;
					int result = db.getUpdate(subcatQuery);
					if(result > 0)
					{
						System.out.println("Updated");
					}
					else
					{
						System.out.println("Not Updated");
					}
				}
				
				i = i + varieties;
				System.out.println("i is "+i);
			}*/
		} 
		catch(Exception e)
		{
			e.printStackTrace();
		}
	}
}